/*
 * 
 * 
 * 
 */
package com.hboxs.asl.dao.impl;

import com.hboxs.asl.dao.NavigationDao;
import com.hboxs.asl.entity.Navigation;
import com.hboxs.asl.entity.Navigation.Position;
import org.springframework.stereotype.Repository;

import javax.persistence.FlushModeType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * Dao - 导航
 */
@Repository("navigationDaoImpl")
public class NavigationDaoImpl extends BaseDaoImpl<Navigation, Long> implements NavigationDao {

    public List<Navigation> findList(Position position) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Navigation> criteriaQuery = criteriaBuilder.createQuery(Navigation.class);
        Root<Navigation> root = criteriaQuery.from(Navigation.class);
        criteriaQuery.select(root);
        if (position != null) {
            criteriaQuery.where(criteriaBuilder.equal(root.get("position"), position));
        }
        criteriaQuery.orderBy(criteriaBuilder.asc(root.get("order")));
        return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
    }

}